package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.BLicenseTypeService;
import cn.huiyunche.base.service.mappers.BLicenseTypeMapper;
import cn.huiyunche.base.service.mappers.DUserLicenseMapper;
import cn.huiyunche.base.service.model.BLicenseType;
import cn.huiyunche.base.service.model.BLicenseTypeExample;
import cn.huiyunche.base.service.model.DUserLicense;
import cn.huiyunche.base.service.model.DUserLicenseExample;
import cn.huiyunche.base.service.vo.BLicenseTypeVo;
import cn.huiyunche.base.service.vo.Result;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BLicenseTypeServiceImpl implements BLicenseTypeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BLicenseTypeServiceImpl.class);

    @Autowired
    private BLicenseTypeMapper bLicenseTypeMapper = null;

    @Autowired
    private DUserLicenseMapper dUserLicenseMapper = null;

    private DUserLicenseMapper getDUserLicenseMapper() {
        return this.dUserLicenseMapper;
    }

    private BLicenseTypeMapper getBLicenseTypeMapper() {
        return this.bLicenseTypeMapper;
    }

    @Override
    public Result<List<BLicenseTypeVo>> list(Long dUserId) throws Exception {
        Result<List<BLicenseTypeVo>> result = new Result<>(true, null, "数据加载成功");
        // 查询所有驾照
        List<BLicenseType> bLicenseTypes = this.getBLicenseTypeMapper().selectByExample(new BLicenseTypeExample());
        // 查询当前用户驾照
        List<DUserLicense> dLicenses = new ArrayList<>();
        if (dUserId != null) {
            dLicenses = this.selectLincese(dUserId);
        }
        List<Integer> ids = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(dLicenses)) {
            ids = dLicenses.stream().mapToInt(v -> v.getSysLicenseId()).collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
        }
        List<BLicenseTypeVo> vos = new ArrayList<>();
        bLicenseTypes.stream().forEach(v -> {
            BLicenseTypeVo vo = new BLicenseTypeVo();
            try {
                PropertyUtils.copyProperties(vo, v);
            } catch (Exception e) {
                LOGGER.error("BLicenseTypeVo copy error: {}", e);
                throw new BusinessException("对象转换异常");
            }
            vos.add(vo);
        });
        for (int i = 0; i < vos.size(); i++) {
            if (ids.contains(vos.get(i).getId())) {
                vos.get(i).setIsCheck(true);
            } else {
                vos.get(i).setIsCheck(false);
            }
        }
        result.setData(vos);
        return result;
    }

    /**
     * 查询用户驾照
     *
     * @param id
     * @return
     * @throws BusinessException
     */
    private List<DUserLicense> selectLincese(Long id) throws Exception {
        DUserLicenseExample example = new DUserLicenseExample();
        example.createCriteria().andUserIdEqualTo(id);
        return this.getDUserLicenseMapper().selectByExample(example);
    }

    public BLicenseType selectByPrimaryKey(Integer id) {
        return bLicenseTypeMapper.selectByPrimaryKey(id);
    }

    public List<BLicenseType> selectByExample(BLicenseTypeExample example) {
        return bLicenseTypeMapper.selectByExample(example);
    }

}
